using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class LootBAG : MonoBehaviour
{
    public GameObject DroppedItemPrefab;
    public List<Loot> lootList = new List<Loot>();

    List<Loot> GetDroppedItems()
    {
        List<Loot> droppedItems = new List<Loot>();
        foreach (Loot item in lootList)
        {
            int randomNumber = Random.Range(0, 101);
            if (randomNumber <= item.DropChance)
            {
                droppedItems.Add(item);
            }
        }
        return droppedItems;
    }

    public void InstantiateLoot()
    {
        List<Loot> droppedItems = GetDroppedItems();
        if (droppedItems != null && droppedItems.Count > 0)
        {
            foreach (Loot item in droppedItems)
            {
                GameObject lootGameObject = Instantiate(DroppedItemPrefab, transform.position, Quaternion.identity);
                var droppedItem = lootGameObject.AddComponent<DroppedItem>();
                droppedItem.itemId = item.itemId;
                SpriteRenderer spriteRenderer = lootGameObject.GetComponent<SpriteRenderer>();
                if (spriteRenderer != null)
                {
                    spriteRenderer.sprite = item.lootSprite;
                    lootGameObject.name = item.lootName;
                }
            }
        }
    }
}
